import { profile } from '@/utils/decorators';
import { runIntervally } from '@/utils/functions/inInterval';
import { Logger } from '@/utils/logger';
import { Colony } from '../../Colony';
import { StructureDistributeManager } from './mods/distribute';
import { HarvestModule } from './mods/harvest';
import { manageCreepSpawning, SpawnModule } from './mods/spawn';
import { TransportModule } from './mods/transport';
import { UpgradeModule } from './mods/upgrade';
import { WorkModule } from './mods/work';
import { RoleCounter } from './RoleCounter';

export class NormalColony extends Colony {

    // 快捷角色数量统计
    readonly roleCounter: RoleCounter;

    // 建筑分布
    readonly structMgr: StructureDistributeManager;

    // 模块
    readonly harvestModule: HarvestModule;
    readonly spawnModule: SpawnModule;
    readonly transportModule: TransportModule;
    readonly upgradeModule: UpgradeModule;
    readonly workModule: WorkModule;

    constructor(capitalRoom: string, outpostNames: string[]) {
        super(capitalRoom, outpostNames);
        // 初始化 managers
        this.roleCounter = new RoleCounter(this);
        this.structMgr = new StructureDistributeManager(this);
        // 初始化模块
        // note! 优先级不能错！
        this.spawnModule = new SpawnModule(this, 'spawn');
        this.harvestModule = new HarvestModule(this, 'harvest');
        this.transportModule = new TransportModule(this, 'transport');
        this.upgradeModule = new UpgradeModule(this, 'upgrade');
        this.workModule = new WorkModule(this, 'work');
    }

    @profile
    execute() {
        // manager
        if (!this.capital.memory.distribute) {
            this.structMgr.update();
        }
        runIntervally(IntervalLength.short, () => this.structMgr.update());
        manageCreepSpawning(this);
        // 模块的调用
        this.spawnModule.handle();      // 处理 spawn 请求
        this.harvestModule.handle();    // 挖 source 和 mineral
        this.transportModule.handle();
        this.upgradeModule.handle();
        this.workModule.handle();
    }
}